import 'package:flutter/material.dart';
import 'package:wallpaper/api/mohu.dart';
import 'package:wallpaper/components/placeholder/appbar_base.dart';
import 'package:wallpaper/components/images/image_list.dart';
import 'package:wallpaper/model/image_base.dart';

class MohuImgs extends StatefulWidget {
  final String type;
  final String title;
  const MohuImgs({super.key, required this.type, required this.title});

  @override
  State<MohuImgs> createState() => _MohuImgsState();
}

class _MohuImgsState extends State<MohuImgs> {
  final List<ImageBase> imgs = [];
  final Set<String> imgUrls = <String>{};
  bool loading = false;
  // 横屏壁纸为主
  List pcs = [
    'pc',
    'img1',
    'img2',
    'gqbz',
    'cat',
    'yzk',
    'sao',
    'yaowei',
    '5huajia',
    'rem',
    'aharen',
    '02',
    'aniya'
  ];

  Future<void> getImgs() async {
    setState(() {
      loading = true;
    });
    if (!mounted) return;
    var res = await mohuImgsApi(id: widget.type);
    if (res.data['code'] == 200 || res.data['code'] == '200') {
      for (var i = 0; i < res.data['pic'].length; i++) {
        ImageBase img = ImageBase(
            smallPath: res.data['pic'][i],
            largePath: res.data['pic'][i].replaceAll('mw690', 'large'),
            tags: '');
        // 判断是否存在
        if (!imgUrls.contains(img.smallPath)) {
          setState(() {
            imgUrls.add(img.smallPath);
            imgs.add(img);
          });
        }
      }
    }
    setState(() {
      loading = false;
    });
  }

  @override
  void initState() {
    super.initState();
    getImgs();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppbarBase(title: widget.title),
      body: ImageList(
          imageList: imgs,
          isLoading: loading,
          isPC: pcs.contains(widget.type),
          onImageBottomLoading: () {
            if (loading) return;
            getImgs();
          },
          onImageListRefresh: () {
            setState(() {
              imgs.clear();
              imgUrls.clear();
            });
            getImgs();
          }),
    );
  }
}
